package com.srit.core.struts2.action;

import java.lang.reflect.Field;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts2.ServletActionContext;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.ServletResponseAware;
import org.apache.struts2.interceptor.SessionAware;
import org.springframework.beans.factory.annotation.Autowired;

import com.opensymphony.xwork2.ActionSupport;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;
import com.opensymphony.xwork2.Validateable;
import com.srit.core.bean.Page;
import com.srit.core.constant.SystemConstant;
import com.srit.core.service.BaseService;

/**
 * crud基础类
 * 
 * @author guan
 * 
 */
public abstract class CrudAction extends ActionSupport implements
		ServletRequestAware, ServletResponseAware, SessionAware, Preparable,
		Validateable, ModelDriven {
	@Autowired(required = true)
	private BaseService baseService;
	protected Page page = new Page();
	protected Map session;
	protected HttpServletRequest request;
	protected HttpServletResponse response;
	protected String uploadPath = ServletActionContext.getServletContext()
			.getRealPath(SystemConstant.FILE_UPLOAD_PATH);
	protected String downloadPath = ServletActionContext.getServletContext()
			.getRealPath(SystemConstant.FILE_DOWNLOAD_PATH);

	// 绝对路径指到webapp上:ServletActionContext.getServletContext().getRealPath("/")
	// "/db":ServletActionContext.getServletContext().getContextPath();

	private String actionName;

	// 绝对路径指到webapp上:ServletActionContext.getServletContext().getRealPath("/")
	// "/db":ServletActionContext.getServletContext().getContextPath();
	public String getActionName() {
		return actionName;
	}

	public Page getPage() {
		return page;
	}

	public void setPage(Page page) {
		this.page = page;
	}

	@Override
	public void prepare() throws Exception {
		if (getModel() != null) {
			Class clz = getModel().getClass();
			actionName = clz.getName()
					.substring(clz.getName().lastIndexOf(".") + 1).trim();
		}

	}

	public void prepareForm() {

	}

	public String form() throws SecurityException, NoSuchFieldException,
			NoSuchMethodException, IllegalArgumentException,
			IllegalAccessException, InvocationTargetException {
		Object obj = getModel();
		Class clz = obj.getClass();
		Field field = clz.getDeclaredField("id");
		// String setName = "set" + field.getName().substring(0).toUpperCase()
		// + field.getName().substring(1);
		String getName = "get" + field.getName().substring(0, 1).toUpperCase()
				+ field.getName().substring(1);
		Method getMethod = clz.getMethod(getName, new Class[] {});
		Object value = getMethod.invoke(obj, new Object[] {});
		if (value != null) {
			Integer intValue = (Integer) value;
			page.getResult().put(
					clz.getName().substring(clz.getName().lastIndexOf(".") + 1)
							.trim(), baseService.getById(getModel(), intValue));
		}
		return "form";
	}

	public void prepareSave() {
		
	}

	public String save() {
		baseService.save(getModel());
		return "save";
	}

	public void prepareUpdate() {

	}

	public String update() {
		baseService.update(getModel());
		return "update";
	}

	public void preparePage() {

	}

	public String page() {
		baseService.page(getModel(), page);
		return "page";
	}

	public void prepareDelete() {

	}

	public String delete() {
		baseService.delete(getModel());
		return "delete";
	}

	@Override
	public void setSession(Map<String, Object> session) {
		this.session = session;
	}

	@Override
	public void setServletResponse(HttpServletResponse response) {
		this.response = response;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;
	}

}
